package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.example.demo.entity.finance;
import com.example.demo.entity.inventory;
import com.example.demo.service.inventoryserviceInter;
import com.example.demo.service.saleserviceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sale")
public class salecontroller {
    @Autowired
    saleserviceInter saledao;
    @Autowired
    inventoryserviceInter inventorydao;

    @RequestMapping("/insert")
    public int insertdate(String saledata, String financedata) {
        finance fin = new finance();
        List<inventory> i = JSON.parseArray(saledata, inventory.class);
        List<finance> f = new ArrayList<>();
        int num = 0;
        int bool1 = 0;
        int bool2 = 0;
        int bool = 0;
        double price = 0;
        double result = 0;

        for (inventory in: i) {
            num = in.getInventorynum();
            price = in.getInventoryprice();
//            System.out.println(num);
//            System.out.println(price);
            result = num * price;

            fin.setCommodityid(in.getInventoryid());
            fin.setCommodityname(in.getInventoryname());
            fin.setCommoditynum(in.getInventorynum());
            fin.setCommodityprice(in.getInventoryprice());
            fin.setTotalprice(result);
            fin.setFinancedata(financedata);
            fin.setDataofproduced(in.getDataofproduced());
            fin.setDatabaozhi(in.getDatabaozhi());
            fin.setManufacturer(in.getManufactory());
            fin.setCommodityaddress(in.getInventoryaddress());

            f.add(fin);

            bool2 = inventorydao.minusnum(in);

            if (bool2 == 1) {
                bool1 = saledao.insertdate(fin);

                if (bool1 ==1) {
                    bool = 1;
                }
            }
            System.out.println(fin);
        }

        System.out.println(i.size());
        return bool;
    }


}
